package com.htn.common.core.util;

import cn.hutool.json.JSONException;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;

import javax.xml.ws.http.HTTPException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

/**
 * @author WD
 * @version 1.0
 * @description: TODO
 * @date 2021/1/25 11:01
 */
public class SendSmsUtil {

	public static String sendSmsCode(int appid, String appkey, int templateId, String smsSign, String username) {
		String code = "";
		try {
			Random random = new Random();
			for (int i = 0; i < 4; i++) {
				code += random.nextInt(10);
			}
			String[] params = {code};
			SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
			SmsSingleSenderResult result = ssender.sendWithParam("86", username, templateId, params, smsSign, "", "");
			System.out.println(result);
		} catch (HTTPException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (com.github.qcloudsms.httpclient.HTTPException e) {
			e.printStackTrace();
		}
		return code;
	}
	public static void sendBizOrderSmsCode(int appid, String appkey, String msg,int templateId, String smsSign, String username) {
		try {
			SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
			String[] params = {msg};
			SmsSingleSenderResult result = ssender.sendWithParam("86", username, templateId, params, smsSign, "", "");
			System.out.println(result);
		} catch (HTTPException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (com.github.qcloudsms.httpclient.HTTPException e) {
			e.printStackTrace();
		}
	}
	public static void sendBizOrderForceSms(int appid, String appkey, String msg,String msgs,int templateId, String smsSign, String username) {
		try {
			SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
			String[] params = {msg,msgs};
			SmsSingleSenderResult result = ssender.sendWithParam("86", username, templateId, params, smsSign, "", "");
			System.out.println(result);
		} catch (HTTPException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (com.github.qcloudsms.httpclient.HTTPException e) {
			e.printStackTrace();
		}
	}



	//提前俩小时 提醒司机出发
	public static void sendDriverSmsCode(int appid, String appkey, String msg,int templateId, String smsSign, String username) {
		try {
			SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
			SmsSingleSenderResult result = ssender.sendWithParam("86", username, templateId, new ArrayList<>(), smsSign, "", "");
			System.out.println(result);
		} catch (HTTPException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (com.github.qcloudsms.httpclient.HTTPException e) {
			e.printStackTrace();
		}
	}


	public static String sendItemCode(int appid, String appkey, int templateId, String smsSign, String username,String itemIdentifyNum) {
		String code = "";
		try {
			Random random = new Random();
			for (int i = 0; i < 6; i++) {
				code += random.nextInt(10);
			}
			String[] params = {code};
			SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
			SmsSingleSenderResult result = ssender.sendWithParam("86", username, templateId, params, smsSign, "", "");
			System.out.println(result);
		} catch (HTTPException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (com.github.qcloudsms.httpclient.HTTPException e) {
			e.printStackTrace();
		}
		return code;
	}

	public static String sendOrderCoIdentifyCode(int appid, String appkey, int templateId, String smsSign, String username,String itemIdentifyNum) {
		String code = "";
		try {
			Random random = new Random();
			for (int i = 0; i < 6; i++) {
				code += random.nextInt(10);
			}
			String[] params = {itemIdentifyNum};
			SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
			SmsSingleSenderResult result = ssender.sendWithParam("86", username, templateId, params, smsSign, "", "");
			System.out.println(result);
		} catch (HTTPException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (com.github.qcloudsms.httpclient.HTTPException e) {
			e.printStackTrace();
		}
		return code;
	}

	public static String sendSmsWithParam(int appid, String appkey, String[] params,int templateId, String smsSign, String username) {
		String code = "";
		try {

			SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
			SmsSingleSenderResult result = ssender.sendWithParam("86", username, templateId, params, smsSign, "", "");
			System.out.println(result);
		} catch (HTTPException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (com.github.qcloudsms.httpclient.HTTPException e) {
			e.printStackTrace();
		}
		return code;
	}



	public static void main(String[] args) {
		sendSmsCode(1400465279,"803e3943dc9e164396bbccfc64aa1a8f",974683,"","13520391225");
	}
}
